package com.example.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author:郭
 * @Description: TODO
 * @Date: 2022/12/16 15:56
 * @ClassName UploadUtils
 * 上传图片工具类
 */
public class UploadUtils {
    public static String uploadImage(MultipartFile multipartFile){
        try {
            String realfilename = multipartFile.getOriginalFilename();
            String imgSuffix = realfilename.substring(realfilename.lastIndexOf("."));
            String newFilename = UUID.randomUUID() + imgSuffix;
            //上传到哪个路径下
            File targetPath = new File("D:\\wenjian\\项目\\思云\\项目\\Sp_Vue\\SiyunVue\\siyun\\src\\assets\\");
            if (!targetPath.exists()){
                targetPath.mkdirs();
            }
            File targetFilename = new File(targetPath,newFilename);
            multipartFile.transferTo(targetFilename);
            //资源映射路径
            return "assets/" + newFilename;
        } catch(IOException e){
            e.printStackTrace();
            return "失败";
        }
    }
}
